package REVIEW2;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient {
    private Socket socket=null;
    public TcpEchoClient(String serverIp,int serverPort) throws IOException {
        socket=new Socket(serverIp,serverPort);
    }
    public void start() throws IOException {
        Scanner scanner=new Scanner(System.in);
        try(InputStream inputStream=socket.getInputStream();
            OutputStream outputStream=socket.getOutputStream()){
            Scanner scanner1=new Scanner(inputStream);
            PrintWriter printWriter=new PrintWriter(outputStream);
            while(true){
                System.out.println("请输入请求");
                String request=scanner.next();
                //1.发送请求
                printWriter.println(request);
//                printWriter.write(request);
                printWriter.flush();
                //2.接收响应
                String response=scanner1.next();
                //3.在控制台上打印出响应
                System.out.println(response);

            }
        }

    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient tcpEchoClient=new TcpEchoClient("127.0.0.1",7777);
        tcpEchoClient.start();
    }

}
